package com.pureland.ucc.config.oauth;

import com.pureland.security.exceptions.InvalidException;
import com.pureland.ucc.service.impl.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 * @projectName: pureland_cloud
 * @className: com.pureland.ucc.config.oauth.PasswordTokenGranter
 * @description: 短信验证码登录令牌授予者实现
 * @author: tong.li
 * @createTime: 2020/11/19 15:40
 * @version: v1.0
 * @copyright: 版权所有 李彤 © 2020
 */
public class PasswordTokenGranter extends AbstractCustomTokenGranter {

    private UserService userService;

    public PasswordTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, UserService userService) {
        super(tokenServices, clientDetailsService, requestFactory, "password");
        this.userService = userService;
    }

    @Override
    protected UserDetails getUser(Map<String, String> parameters)  throws InvalidException  {
        return userService.loadUserByPassword(parameters.get("username"),parameters.get("password"));
    }
}
